View Javadoc
1 package com.fc.taglibs.castor.integration.tomcat; 2 3 import java.io.PrintWriter; 4 import java.io.StringWriter; 5 import java.io.Serializable; 6 import java.lang.reflect.Method; 7 import java.util.Enumeration; 8 import java.util.Hashtable; 9 import javax.naming.Context; 10 import javax.naming.InitialContext; 11 import javax.naming.Name; 12 import javax.naming.NamingException; 13 import javax.naming.RefAddr; 14 import javax.naming.Reference; 15 import javax.naming.spi.ObjectFactory; 16 17 import javax.transaction.TransactionManager; 18 19 import org.xml.sax.InputSource; 20 21 import org.exolab.castor.jdo.JDO; 22 import org.exolab.castor.jdo.Database; 23 import org.exolab.castor.jdo.conf.Database; 24 import org.exolab.castor.mapping.MappingException; 25 import org.exolab.castor.xml.Unmarshaller; 26 import org.exolab.castor.persist.spi.LogInterceptor; 27 28 29 public class CastorJdoObjectFactory implements ObjectFactory,LogInterceptor 30 { 31 /* 32 * Declare loggers (if needed) 33 */ 34 private PrintWriter writer; 35 36 /* 37 * The JDO properties set from the 38 * attributes in our server.xml 39 */ 40 private String _config = null; 41 private String _description = null; 42 private int _lockTimeout = 0; 43 private boolean _loggingEnabled = false; 44 private boolean _commonClasspath = false; 45 private boolean _autoStore = false; 46 private boolean _databasePooling = false; 47 private String _transactionManager = null; 48 49 public Object getObjectInstance(Object obj, 50 Name name, Context nameCtx, Hashtable environment) 51 throws NamingException 52 { 53 /* 54 * Declare the JDO and related Castor bits we need. 55 */ 56 JDO jdo = null; 57 org.exolab.castor.jdo.conf.Database database = null; 58 Unmarshaller unmarshaller = null; 59 60 61 62 63 /* 64 * Retrieve the JDO properties from our attributes 65 */ 66 Reference ref = (Reference) obj; 67 Enumeration addrs = ref.getAll(); 68 while (addrs.hasMoreElements()) { 69 RefAddr addr = (RefAddr) addrs.nextElement(); 70 String attName = addr.getType(); 71 String attValue = (String) addr.getContent(); 72 if (attName.equals("Configuration")) 73 { 74 _config = attValue; 75 } 76 else if (attName.equals("Description")) 77 { 78 _description = attValue; 79 } 80 else if (attName.equals("LockTimeout")) 81 { 82 try { 83 _lockTimeout = Integer.parseInt(attValue); 84 } catch (NumberFormatException e) { 85 throw new NamingException("CastorJdoObjectFactory: Invalid 'LockTimeout' value " + attValue); 86 } 87 } 88 else if (attName.equals("LoggingEnabled")) 89 { 90 _loggingEnabled = new Boolean(attValue).booleanValue(); 91 } 92 else if (attName.equals("CommonClasspath")) 93 { 94 _commonClasspath = new Boolean(attValue).booleanValue(); 95 } 96 else if (attName.equals("AutoStore")) 97 { 98 _autoStore = new Boolean(attValue).booleanValue(); 99 } 100 else if (attName.equals("DatabasePooling")) 101 { 102 _databasePooling = new Boolean(attValue).booleanValue(); 103 } 104 else if (attName.equals("TransactionManager")) 105 { 106 _transactionManager = attValue; 107 } 108 109 } 110 111 112 try 113 { 114 /* 115 * Create the JDO instance 116 */ 117 System.out.println("Castor JDO: Creating new JDO instance." ); 118 System.out.println("Configuration file : " + _config ); 119 System.out.println("Description : " +_description ); 120 System.out.println("Lock timeout: " +_lockTimeout ); 121 System.out.println("Logging enabled : " +_loggingEnabled ); 122 System.out.println("Common classpath : " +_commonClasspath ); 123 System.out.println("Autostore : " +_autoStore ); 124 System.out.println("Database pooling : " +_databasePooling ); 125 System.out.println("Transaction manager : " +_transactionManager ); 126 127 jdo = new JDO(); 128 jdo.setClassLoader( getClass().getClassLoader() ); 129 /* 130 * We assume that the database is in WEB-INF/classes/ 131 * and load it as a resource. The name of the database.xml 132 * is got from the server.xml attribute. 133 */ 134 jdo.setConfiguration(getClass().getResource("/" + _config).toString()); 135 unmarshaller = new Unmarshaller(org.exolab.castor.jdo.conf.Database.class); 136 database = (org.exolab.castor.jdo.conf.Database) unmarshaller.unmarshal(new InputSource(getClass().getResource("/" + _config).toString())); 137 jdo.setDatabaseName(database.getName()); 138 139 /* 140 * Set the other JDO properties from our attributes 141 */ 142 jdo.setDescription(_description); 143 144 if (!_commonClasspath) 145 jdo.setClassLoader(null); 146 147 148 if (_loggingEnabled) 149 { 150 jdo.setLogInterceptor( this ); 151 } 152 else 153 { 154 jdo.setLogInterceptor( null ); 155 } 156 157 jdo.setLockTimeout(_lockTimeout); 158 /* 159 * Older Castor versions older don't have these methods, 160 * we'll use reflection for backward compatibility 161 * jdo.setAutoStore(_autoStore); 162 * jdo.setDatabasePooling(_dbpooling); 163 * Thanks Oleg Nitz! 164 */ 165 166 Method m; 167 try 168 { 169 // 0.9.4 170 m = jdo.getClass().getMethod( "setAutoStore", 171 new Class[] {boolean.class}); 172 m.invoke(jdo, new Object[] {new Boolean(_autoStore)}); 173 } 174 catch (Exception ex) 175 { 176 177 } 178 179 try 180 { 181 // 0.9.3 182 m = jdo.getClass().getMethod( "setDatabasePooling", 183 new Class[] {boolean.class}); 184 m.invoke(jdo, new Object[] {new Boolean(_databasePooling)}); 185 } 186 catch (Exception ex) 187 { 188 189 } 190 191 192 if (_transactionManager!=null) 193 { 194 jdo.setTransactionManager( _transactionManager ); 195 } 196 197 198 } 199 catch (Exception e) 200 { 201 System.out.println("Castor JDO: Exception thrown in CastorJdoObjectFactory: " + e.getMessage()); 202 throw new NamingException(e.getMessage()); 203 } 204 // Return the customized instance 205 return (jdo); 206 207 } 208 209 // LogInterceptor implementation for Castor 0.8 ---------------------- 210 public void loading(Class objClass, Object identity) 211 { 212 if (_loggingEnabled) 213 { 214 System.out.println("Castor JDO: Storing " + objClass.getName() + " (" + identity + ")" ); 215 } 216 } 217 218 219 public void creating(Class objClass, Object identity) 220 { 221 if (_loggingEnabled) 222 { 223 System.out.println("Castor JDO: Storing " + objClass.getName() + " (" + identity + ")" ); 224 } 225 } 226 227 228 public void removing(Class objClass, Object identity) 229 { 230 if (_loggingEnabled) 231 { 232 System.out.println("Castor JDO: Storing " + objClass.getName() + " (" + identity + ")" ); 233 } 234 } 235 236 237 public void storing(Class objClass, Object identity) 238 { 239 if (_loggingEnabled) 240 { 241 System.out.println("Castor JDO: Storing " + objClass.getName() + " (" + identity + ")" ); 242 } 243 } 244 245 246 // LogInterceptor implementation for Castor 0.9 ---------------------- 247 public void loading(Object objClass, Object identity) 248 { 249 if (_loggingEnabled) 250 { 251 System.out.println("Castor JDO: Storing " + objClass + " (" + identity + ")" ); 252 } 253 } 254 255 256 public void creating(Object objClass, Object identity) 257 { 258 if (_loggingEnabled) 259 { 260 System.out.println("Castor JDO: Storing " + objClass + " (" + identity + ")" ); 261 } 262 } 263 264 265 public void removing(Object objClass, Object identity) 266 { 267 if (_loggingEnabled) 268 { 269 System.out.println("Castor JDO: Storing " + objClass + " (" + identity + ")" ); 270 } 271 } 272 273 274 public void storing(Object objClass, Object identity) 275 { 276 if (_loggingEnabled) 277 { 278 System.out.println("Castor JDO: Storing " + objClass + " (" + identity + ")" ); 279 } 280 } 281 282 283 // LogInterceptor implementation 284 285 public void storeStatement(String statement) 286 { 287 if (_loggingEnabled) 288 { 289 System.out.println("Castor JDO: " + statement); 290 } 291 } 292 293 294 public void queryStatement(String statement) 295 { 296 if (_loggingEnabled) 297 { 298 System.out.println("Castor JDO: " + statement); 299 } 300 } 301 302 303 public void message(String message) 304 { 305 if (_loggingEnabled) 306 { 307 System.out.println("Castor JDO: " + message); 308 } 309 } 310 311 312 public void exception(Exception except) 313 { 314 if (_loggingEnabled) 315 { 316 System.out.println("Castor JDO: " + except.getMessage()); 317 } 318 } 319 320 public PrintWriter getPrintWriter() 321 { 322 if (writer == null) 323 { 324 StringWriter sw = new StringWriter(); 325 writer = new PrintWriter(sw); 326 } 327 return writer; 328 } 329 330 }

This page was automatically generated by Maven